<?php
	class RegisterHandler
	{
		// Membervariables
		private $_userHandler = null;
		private $_validate = null;
		private $_salt = null;
		
		// Constants
		const USERNAMEXSISTS = "A user with that name already exists.";
		const PASSWORDDIDNTMATCH = "Your passwords didn't match.";
		const INPUTCONTAINSHTML = "The userinput contained HTML check time and ip.";
		const INPUTWASEMPTY = "Don't leave a field empty.";
		const INVALIDEMAIL = "Bad email, please input a valid email-adress to continue.";
		const INVALIDPASSWORD = "Password didn't match the rules, make sure your password <br/>is not to short and contains atleast 3 of the following:<br />Capital letters.<br />Small letters.<br />Numbers.<br />Special-symbols.";
		
		public function __construct(\UserHandler $userhandler)
		{
			$this->_userHandler = $userhandler;
			$this->_validate = new Validation();
		}
		
		// Register the user given as a parameter.
		public function DoRegisterUser(\User $user)
		{
			$this->_userHandler->CreateNewUser($user);
		}
		
		// Filter away tags and white-spaces from the user inputs.
		public function FilterUserInput(Array $userInfo)
		{
			foreach($userInfo as $key => $value)
			{
				$userInfo[$key] = $this->_validate->StripAll($value);
				$userInfo[$key] = trim($value);
			}
			
			return $userInfo;
		}
		
				// Generates a user object from the user info given as a parameter.
		public function GenerateUserobjectFromUserinfo(Array $userInfo)
		{
			$user = new User();
			
			// Generate a salt for the password.
			$this->_salt = $this->_userHandler->GenerateSalt(32);
			
			// Transfer the data from the array to a user-object
			$user->id = $this->GenerateUserId();
			$user->username = $userInfo[RegisterView::FORMUSERNAME];
			$user->password = $userInfo[RegisterView::FORMPASSWORD] . $this->_salt; // Set the password and add the salt.
			$user->salt = $this->_salt;
			if(isset($userInfo[RegisterView::USERTYPE]) == true)
			{
				$user->usertype = $userInfo[RegisterView::USERTYPE];
			}
			else 
			{
				$user->usertype = 0;
			}
			$user->givenname = $userInfo[RegisterView::FORMGIVENNAME];
			$user->surname = $userInfo[RegisterView::FORMSURNAME];
			$user->email = $userInfo[RegisterView::FORMEMAIL];
			
			// Encrypt the password.
			$user->password = $this->_userHandler->Encrypt($user->password);

			return $user;
		}	
		
		// Validates all the user info.
		public function ValidateInput(Array $userInfo)
		{			
			// Iterate through the array to see if any of the elements are empty or null.
			foreach($userInfo as $value)
			{
				if($this->_validate->IsInputEmpty($value) == true)
				
				return RegisterHandler::INPUTWASEMPTY;
			}
			
			// Check if the user has input any HTML or any other scripts/tags.
			if ($this->DoesArrayContainHTML($userInfo) == true)
			{
				return RegisterHandler::INPUTCONTAINSHTML;
			}
			
			// Check wether the passwords matched.
			if($userInfo[RegisterView::FORMPASSWORD] != $userInfo[RegisterView::FORMCONFIRMPASSWORD])
			{
				return RegisterHandler::PASSWORDDIDNTMATCH;
			}
			
			// Check if the user already exists and if so gives an error.
			if($this->_userHandler->GetUser($userInfo[RegisterView::FORMUSERNAME]) != null)
			{
				return RegisterHandler::USERNAMEXSISTS;
			}
			
			// Check wether the password was valid or not.
			if($this->_validate->ValidatePassword($userInfo[RegisterView::FORMPASSWORD]) == false)
			{
				return RegisterHandler::INVALIDPASSWORD;	
			}
			
			// Check wether the user entered a valid email.
			if($this->_validate->ValidateEmail($userInfo[RegisterView::FORMEMAIL]) == false)
			{
				return RegisterHandler::INVALIDEMAIL;	
			}
			
			return null;	
		}
		
		// Returns wether the array contains html, scripts or tags.
		public function DoesArrayContainHTML(Array $array)
		{
			$temp = null;
			foreach($array as $key => $value)
			{
				$temp = $this->_validate->StripAll($value);
				if ($temp != $value)
				{
					return true;
				}
			}
			return false;
		}
		
		// Generates a new unique userid.
		private function GenerateUserId()
		{
			// Get a list of all the users.
			$userList = $this->_userHandler->GetUserList();
			$newId = 0;
			
			foreach ($userList as $user)
			{
				if ($newId == $user->id)
				{
					$newId++;
				}
				else 
				{
					return $newId;
				}
			}
			
			return $newId;
		}		
	}		